﻿using Serilog;
using Serilog.Core;
using System;
using System.Collections.Generic;

namespace WeatherComm
{
    public static class Log
    {
        public static int Mode { get; set; }
        public static string FileName { get; set; } = "LogFile.txt";
        public static string Path { get; set; } = $@"{Environment.CurrentDirectory}\\Log";

        /// <summary>
        ///     是否启用日志记录
        ///     0:不记录
        ///     1:记录所有
        ///     2:只记录Information和异常
        ///     3:只记录error和异常
        ///     4:只记录异常
        /// </summary>
        public static int IsLog { get; set; } = 1;

        private static Logger _logger;
        private static readonly LoggerConfiguration _logSetting;
        private static readonly Dictionary<string, Logger> _loggers = new Dictionary<string, Logger>();

        private static void WriteInfo(string s, Logger logger)
        {
            if (Mode == 0)
            {
                if (IsLog != 1 && IsLog != 2) return;
                logger?.Information(s);
            }
            else
            {
                if (IsLog != 1 && IsLog != 3) return;
                logger?.Error(s);
            }
        }
        public static void WriteLog(string s, string fileName = "")
        {
            if (IsLog == 0) return;
            try
            {
                var flag = false;
                switch (fileName)
                {
                    case "" when _logger != null:
                        {
                            WriteInfo(s, _logger);
                            return;
                        }
                    case "":
                    case null:
                        flag = true;
                        break;
                }

                LoggerConfiguration a = null;

                if (flag || !_loggers.ContainsKey(fileName))
                {
                    a = new LoggerConfiguration()
                        .MinimumLevel.Verbose()
                        .WriteTo.File($"{Path}\\{(flag ? FileName : fileName)}", rollingInterval: RollingInterval.Day,
                            shared: true,
                            fileSizeLimitBytes: 20000000, rollOnFileSizeLimit: true,
                            retainedFileCountLimit: 200,
                            retainedFileTimeLimit: new TimeSpan(60, 0, 0, 0),
                            outputTemplate:
                            "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {Message:lj}{NewLine}{Exception}"
                        );
                }

                if (flag)
                {
                    _logger = a.CreateLogger();
                    WriteInfo(s, _logger);
                    return;
                }

                //写入自定义文件名日志
                Logger logger;
                if (!_loggers.ContainsKey(fileName))
                {
                    logger = a?.CreateLogger();
                    _loggers.Add(fileName, logger);
                }
                else logger = _loggers[fileName];
                WriteInfo(s, logger);
            }
            catch (Exception e)
            {
                if (Serilog.Log.Logger != null)
                    if (IsLog <= 4)
                        _logger?.Error(e.Message);
            }
        }
    }
}